/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.desktop.paneles;

import com.google.gson.Gson;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 *
 * @author Yanira
 */
public class Expositor extends javax.swing.JPanel
{

    private File image;
    private AdmEvent padre;
    /**
     * Creates new form Expositor
     */
    public Expositor(AdmEvent father) {
        padre = father;
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();

        setMinimumSize(new java.awt.Dimension(602, 548));

        jLabel1.setText("Nombre Expositor:");

        jLabel2.setText("Reseña:");

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jLabel3.setText("Fotografía");

        jButton1.setText("Cargar Foto...");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Cancelar");
        jButton2.setMaximumSize(new java.awt.Dimension(75, 25));
        jButton2.setMinimumSize(new java.awt.Dimension(75, 25));
        jButton2.setPreferredSize(new java.awt.Dimension(75, 25));
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Aceptar");
        jButton3.setMaximumSize(new java.awt.Dimension(75, 25));
        jButton3.setMinimumSize(new java.awt.Dimension(75, 25));
        jButton3.setPreferredSize(new java.awt.Dimension(75, 25));
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel4.setText(".");
        jLabel4.setMaximumSize(new java.awt.Dimension(500, 500));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField1)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 475, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jButton1))
                .addGap(5, 5, 5)
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 321, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        //Selecionar una fotografía del expositor 
        //Cargar el archivo de tipo images,jgp,gif,png
        JFileChooser chooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter(
        "Images", "jpg", "gif","png");
        chooser.setFileFilter(filter);
        int returnVal;
        returnVal = chooser.showOpenDialog(this);   
        if(returnVal == JFileChooser.APPROVE_OPTION) {
            java.io.File fichero = chooser.getSelectedFile();
            ImageIcon im = new ImageIcon(fichero.getAbsolutePath());
            if(im.getIconHeight()<500 && im.getIconWidth()<500)
            {
                jLabel4.setIcon(im);
                this.image = fichero;
            }
            else
            {
                JOptionPane.showMessageDialog(null, "Tamaño no valido");
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        padre.jSplitPane4.setRightComponent(new BienvExpositor(Temporal.evento));
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        //boton aceptar
        if(comprobarExpoEnSistem(jTextField1.getText()))
        {
            JOptionPane.showMessageDialog(this,"Ya existe un expositor con el mismo nombre","Error", JOptionPane.ERROR_MESSAGE);
        }
        else
        {
            if(jTextField1.getText().length()>0 && image!=null)
            {
                String name = jTextField1.getText();
                String resena = jTextArea1.getText();
                if(jTextArea1.getText().length()==0)
                {
                    resena = "Sin reseña";
                }
                FileInputStream ficheroStream = null;
                byte[] contenido = null;
                try {
                    ficheroStream = new FileInputStream(this.image);
                     contenido = new byte[(int) image.length()];
                    ficheroStream.read(contenido);
                }catch (IOException ex) {
                    Logger.getLogger(Expositor.class.getName()).log(Level.SEVERE, null, ex);
                }
                String imagen = new Gson().toJson(contenido);
                this.addExpositor(name, resena, imagen);
                JOptionPane.showMessageDialog(this, "Expositor ingresado al sistema", "Expositor creado", JOptionPane.INFORMATION_MESSAGE);
                jTextField1.setText("");
                jTextArea1.setText("");
                image=null;
                jLabel4.setIcon(null);
                
            }
            else
            {
                JOptionPane.showMessageDialog(this, "Datos incompletos", "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    public boolean comprobarExpoEnSistem(String expositorEntrante)
    {
        Gson gson = new Gson();
        String [] expositoresSistema = gson.fromJson(listaExpositores(), String[].class);
        if(expositoresSistema!=null)
        {
            for(String a:expositoresSistema)
            {
                if(a.equals(expositorEntrante))
                {
                    return true;//si el expositor ya esta en el sistema retorna true
                }
            }  
            return false;
        }
        return false;
    }
    
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    public javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables

    private static void addExpositor(java.lang.String nombre, java.lang.String resena, java.lang.String foto) {
        com.webservice.servicios.Gestor_Service service = new com.webservice.servicios.Gestor_Service();
        com.webservice.servicios.Gestor port = service.getGestorPort();
        port.addExpositor(nombre, resena, foto);
    }

    private static String listaEventos() {
        com.webservice.servicios.Gestor_Service service = new com.webservice.servicios.Gestor_Service();
        com.webservice.servicios.Gestor port = service.getGestorPort();
        return port.listaEventos();
    }

    private static String listaExpositoresEvento(java.lang.String evento, java.lang.String actividad) {
        com.webservice.servicios.Gestor_Service service = new com.webservice.servicios.Gestor_Service();
        com.webservice.servicios.Gestor port = service.getGestorPort();
        return port.listaExpositoresEvento(evento, actividad);
    }

    private static String listaExpositores() {
        com.webservice.servicios.Gestor_Service service = new com.webservice.servicios.Gestor_Service();
        com.webservice.servicios.Gestor port = service.getGestorPort();
        return port.listaExpositores();
    }
    
    
}
